<template>
  <div class="app-container">
    <!-- Filter -->
    <div class="filter-container">
      <el-input v-model="listQuery.name" placeholder="计划名称" style="width: 200px;" class="filter-item" />
      <el-select v-model="listQuery.project" filterable placeholder="请选择" class="filter-item" value="">
        <el-option v-for="item in projectList" :key="item.id" :label="item.name" :value="item.id" />
      </el-select>
      <el-button type="primary" class="filter-item" @click="handleFilter">查询</el-button>
      <el-button type="primary" style="margin-left: 20px" class="filter-item" @click="handleCreate">新增</el-button>
    </div>
    <!-- Table -->
    <el-table v-loading="listLoading" :data="list" border>
      <el-table-column label="ID" prop="id" width="70px">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划名称" prop="name" width="200px">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="测试项目" prop="project_name" width="200px">
        <template slot-scope="scope">
          <span>{{ scope.row.project_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="remark" width="500px"/>
      <el-table-column label="创建时间" prop="add_time" width="200px">
        <template slot-scope="scope">
          <span>{{ dateFormat(scope.row.add_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" prop="edit_time" width="200px">
        <template slot-scope="scope">
          <span>{{ dateFormat(scope.row.edit_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" >
        <template slot-scope="{row}">
          <el-button type="warning" size="small" @click="handleExecute(row)">执行</el-button>
          <el-button type="primary" size="small" @click="handleUpdate(row)">编辑</el-button>
          <router-link :to="'/TestExecute/planCase/list/'+ row.id">
            <el-button type="primary" size="small">计划详情</el-button>
          </router-link>
          <el-button type="danger" size="small" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 展示计划执行的测试报告dialog -->
    <el-dialog :visible.sync="reportDialogVisible" title="测试报告" style="width: 100%">
      <!-- report router link -->
      <el-form :model="reportForm">
        <el-form-item label="报告名称">
          <span>{{ reportForm.name }}</span>
          <el-button v-if="reportForm.id" style="margin-left: 20px" @click="handleDownload(reportForm.id)">下载报告</el-button>
        </el-form-item>
        <el-form-item label="用例总数">
          <span>{{ reportForm.total }}</span>
        </el-form-item>
        <el-form-item label="测试通过个数">
          <span>{{ reportForm.success }}</span>
        </el-form-item>
        <el-form-item label="失败用例个数">
          <span>{{ reportForm.fail }}</span>
        </el-form-item>
      </el-form>
      <el-table :data="caseResultList" border >
        <el-table-column label="用例名称" prop="name">
          <template slot-scope="scope">
            <span>{{ scope.row.name.slice(16) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="用例执行结果" prop="result" width="120px">
          <template slot-scope="scope">
            <el-popover trigger="hover" placement="right">
              <p>日志: {{ scope.row.log }}</p>
              <div slot="reference" class="name-wrapper">
                <el-tag v-if="scope.row.result === 0" type="primary">SUCCESS</el-tag>
                <el-tag v-if="scope.row.result === 1" type="warning">FAILURE</el-tag>
                <el-tag v-if="scope.row.result === 2" type="error">ERROR</el-tag>
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="200px">
          <template slot-scope="scope">
            <span>{{ dateFormat(scope.row.add_time) }}</span>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <!-- pagination -->
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" />
    <!-- 新增/编辑dialog -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="80px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="计划名称" prop="name">
          <el-input v-model="temp.name" />
        </el-form-item>
        <el-form-item label="测试项目" prop="project">
          <el-select v-model="temp.project" class="filter-item" placeholder="请选择">
            <el-option
              v-for="item in projectList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="remark">
          <el-input v-model="temp.remark" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getPlanList, getPlan, createPlan, updatePlan, deletePlan } from '@/api/plan'
import { getCaseResultList } from '@/api/caseResult'
import { getProjectList } from '@/api/project'
import { executePlan } from '@/api/plan'
import { reportPath } from '@/utils/request'
import Pagination from '@/components/Pagination/index'
import { dateFormat } from '@/utils/data'
export default {
  name: 'List',
  components: { Pagination },
  data() {
    return {
      list: [],
      projectList: [],
      total: 0,
      listLoading: false,
      listQuery: {
        name: '',
        project: '',
        page: 1,
        limit: 20
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      temp: {
        id: undefined,
        name: '',
        project: undefined,
        detail: ''
      },
      rules: {
        name: [{ required: true, message: '计划名称不能为空', trigger: 'change' }]
      },
      reportDialogVisible: false,
      reportPath: 'report/check/',
      query: {},
      reportForm: {
        id: undefined,
        name: '',
        total: undefined,
        success: undefined,
        fail: undefined
      },
      caseResultList: [],
      caseResultParams: {
        report: undefined,
        page: 1,
        limit: 20
      },
      caseResultTotal: 0,
      caseStatus: {
        0: 'SUCCESS',
        1: 'FAILURE',
        2: 'ERROR',
        3: 'SKIP'
      }
    }
  },
  created() {
    this.getList()
    this.getProjectList()
  },
  methods: {
    dateFormat: function(attr) {
      return dateFormat(attr)
    },
    handleDownload(id) {
      const url = reportPath(id)
      console.log('reportPath: ', reportPath())
      window.open(url)
    },
    tableRowClassName({ row, rowIndex }) {
      if (row.result === 2) {
        console.log('row.result: ', row.result)
        return 'warning-row'
      } else if (row.result === 0) {
        return 'success-row'
      }
      return ''
    },
    getList() {
      this.listLoading = true
      getPlanList(this.listQuery).then((response) => {
        this.list = response.results
        this.total = response.count
        this.listLoading = false
      })
    },
    getProjectList() {
      const params = {
        page: 1,
        limit: 30
      }
      getProjectList(params).then((response) => {
        this.projectList = response.results
      })
    },
    resetForm() {
      this.temp = {
        id: undefined,
        name: '',
        project: undefined,
        detail: ''
      }
    },
    handleFilter() {
      this.getList()
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleUpdate(row) {
      getPlan(row.id).then(response => {
        this.temp = Object.assign({}, response.data)
      })
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
    },
    handleDelete(row) {
      this.loading = true
      deletePlan(row.id).then(() => {
        this.handleFilter()
      })
    },
    handleExecute(row) {
      executePlan(row.id).then(response => {
        this.listLoading = true
        if (response.code === 200) {
          this.listLoading = false
          this.reportForm = Object.assign({}, response.data)
          this.caseResultParams.report = this.reportForm.id
          getCaseResultList(this.caseResultParams).then(response => {
            this.caseResultList = response.results
            this.caseResultTotal = response.total
          })
          this.reportDialogVisible = true
        } else {
          this.listLoading = false
        }
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          createPlan(this.temp).then(() => {
            this.getList()
            this.dialogFormVisible = false
            this.$notify({
              title: 'success',
              message: '保存成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        const tempDate = Object.assign({}, this.temp)
        updatePlan(tempDate.id, tempDate).then(() => {
          this.getList()
          this.dialogFormVisible = false
          this.$notify({
            title: 'success',
            message: '更新成功',
            type: 'success',
            duration: 2000
          })
        })
      })
    }
  }
}
</script>

<style scoped>
  .warning-row {
    background: #F56C6C;
  }

  .success-row {
    background: #67C23A;
  }
</style>
